package com.zhouzhou.fastjava.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public abstract class MoneyKit {

    private MoneyKit() {
    }

    public static BigDecimal unifyFormat(BigDecimal value) {
        return value.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * ==
     */
    public static boolean equals(BigDecimal left, BigDecimal right) {
        return unifyFormat(left).equals(unifyFormat(right));
    }

    /**
     * !=
     */
    public static boolean notEquals(BigDecimal left, BigDecimal right) {
        return !equals(left, right);
    }

    /**
     * >
     */
    public static boolean gt(BigDecimal left, BigDecimal right) {
        return unifyFormat(left).compareTo(unifyFormat(right)) > 0;
    }

    /**
     * >=
     */
    public static boolean ge(BigDecimal left, BigDecimal right) {
        return unifyFormat(left).compareTo(unifyFormat(right)) >= 0;
    }

    /**
     * >
     */
    public static boolean gt(BigDecimal left, int right) {
        return unifyFormat(left).compareTo(unifyFormat(new BigDecimal(right))) > 0;
    }

    /**
     * >=
     */
    public static boolean ge(BigDecimal left, int right) {
        return unifyFormat(left).compareTo(unifyFormat(new BigDecimal(right))) >= 0;
    }

    /**
     * <
     */
    public static boolean lt(BigDecimal left, BigDecimal right) {
        return !ge(left, right);
    }

    /**
     * <=
     */
    public static boolean le(BigDecimal left, BigDecimal right) {
        return !gt(left, right);
    }

    /**
     * <
     */
    public static boolean lt(BigDecimal left, int right) {
        return !ge(left, right);
    }

    /**
     * <=
     */
    public static boolean le(BigDecimal left, int right) {
        return !gt(left, right);
    }

    /**
     * /
     */
    public static BigDecimal divide(BigDecimal numerator, BigDecimal denominator) {
        return numerator.divide(denominator, 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 向上取整
     */
    public static Integer ceil(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return of(Math.ceil(money.doubleValue())).intValue();
    }

    /**
     * 向上取整（十分位）
     */
    public static Integer ceilOfTen(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return of(Math.ceil(money.doubleValue() / 10) * 10).intValue();
    }

    /**
     * of
     */
    public static BigDecimal of(Integer money) {
        return new BigDecimal(money);
    }

    /**
     * of
     */
    public static BigDecimal of(double money) {
        return unifyFormat(new BigDecimal(money));
    }

    /**
     * of Penny 分
     */
    public static BigDecimal ofPenny(Integer moneyPenny) {
        return of(Double.valueOf(moneyPenny) / 100);
    }

    /**
     * 舍去
     */
    public static BigDecimal down(double money) {
        return new BigDecimal(money).setScale(2, RoundingMode.DOWN);
    }

}
